package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.ServiceConstant;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.application.bean.Topic;
import cn.nubia.admin.application.bean.TopicVo;
import cn.nubia.admin.application.query.TopicQueryParam;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/7/10.
 */
@Service
public class TopicService extends BaseServiceImpl<Topic> {

    @Autowired
    SystemLogService systemLogService;



    public Paged<TopicVo> findPageList(TopicQueryParam topicQuery) {
        Query<Topic> query = Query.build(Topic.class);
        query.setPaged(topicQuery.getPageNo(), topicQuery.getPageSize());
        if(topicQuery.getState()!=null &&topicQuery.getState()!=-99){
            query.addEq("state", topicQuery.getState());
        }else{
            query.addNotEq("state", Topic.STATE_DEL);
        }

        query.addLike("topicName", topicQuery.getTopicName());
        query.addEq("id", topicQuery.getTopicId());
        if(topicQuery.getStartTimes()!=null && topicQuery.getStartTimee()!= null){
            query.addBetween(Topic.COLUMN_STARTTIME, DateUtil.getIntegerTime(topicQuery.getStartTimes()), DateUtil.getIntegerTime(topicQuery.getStartTimee()));
        }


        query.addOrder("createTime", Query.DBOrder.DESC);
        Paged<Topic> page = findPagedByQuery(query);

        List<Topic> list = page.getListData();
        Paged<TopicVo> paged = new Paged<TopicVo>();
        List<TopicVo> listd = new ArrayList<TopicVo>();
        for (int i = 0; i < list.size(); i++) {
            Topic topic = list.get(i);
            TopicVo topicVo = new TopicVo();
            BeanUtils.copyProperties(topic, topicVo);
            topicVo.setTopicShowTypeName(ServiceConstant.showTypes.get(topic.getTopicShowType()).getName());
            listd.add(topicVo);
        }
        paged.setListData(listd);
        paged.setPageSize(page.getPageSize());
        paged.setPageNo(page.getPageNo());
        paged.setTotalCount(page.getTotalCount());
        return paged;
    }

    /**
     * @param topic
     */
    public void save(Topic topic) {
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        topic.setLastModifyTime(curr);
        if (null == topic.getId()) {
            topic.setCreateTime(curr);
            topic.setCreateAdminId(adminId);
            topic.setCreateAdminName(adminName);
            insert(topic);
            systemLogService.log(SystemLog.ModuleTypeEnum.TOPICLIST, SystemLog.OperateTypeEnum.ADD, topic.getId());

        } else {
            Topic domain = get(topic.getId());
            topic.setCreateAdminName(domain.getCreateAdminName());
            topic.setCreateTime(domain.getCreateTime());
            topic.setCreateAdminId(domain.getCreateAdminId());

            topic.setModifyAdminId(adminId);
            topic.setModifyAdminName(adminName);
            update(topic);
            systemLogService.log(SystemLog.ModuleTypeEnum.TOPICLIST, SystemLog.OperateTypeEnum.EDIT, topic.getId());

        }
    }
}
